<?php
require_once("DBFunctions.php");
require_once("getCredentials.php");

class PopulateLists {

	function populateSpecies() {
		$db = new DBFunctions;
		$db->connectDB();
		$species = "";
		$result = $db->query("SELECT Animal.Species_ID, commonName FROM Animal, Species WHERE Animal.Species_ID = Species.Species_ID ORDER BY commonName;");
		if( !$result ) {
			die( "Failed selecting the species." );
		}		
		while( $line = mysqli_fetch_array( $result, MYSQL_ASSOC ) ) {
			$species .= "<option value='".$line['Species_ID']."'>".$line['commonName']."</option>";
		}
		
		print $species;
		
		$db->closeDB();
	}
	
	function populatePlants() {
		$db = new DBFunctions;
		$db->connectDB();
		$species = "";
		$result = $db->query("SELECT Plant.Species_ID, commonName FROM Plant, Species WHERE Plant.Species_ID = Species.Species_ID ORDER BY commonName;");
		if( !$result ) {
			die( "Failed selecting the species." );
		}		
		while( $line = mysqli_fetch_array( $result, MYSQL_ASSOC ) ) {
			$species .= "<option value='".$line['Species_ID']."'>".$line['commonName']."</option>";
		}
		
		print $species;
		
		$db->closeDB();
	}
	
	function populateEnvironmentEvents($email) {
		$gc = new GetCredentials;
		$uid = $gc->getUserID();
		$db = new DBFunctions;
		$db->connectDB();
		$environments = "";
		$result = $db->query("SELECT Environment_ID, Name FROM EnvironmentEvent WHERE User_ID = ".$uid." ORDER BY Name;");
		if( !$result ) {
			die( "Failed selecting the environments." );
		}
		while( $line = mysqli_fetch_array( $result, MYSQL_ASSOC ) ) {
			$environments .= "<option value='".$line['Environment_ID']."'>".$line['Name']."</option>";
		}
		
		print $environments;
		$db->closeDB();
	}
	
	function populateEnvironmentEventsReturn($email) {
		$gc = new GetCredentials;
		$uid = $gc->getUserID();
		$db = new DBFunctions;
		$db->connectDB();
		$environments = "";
		$result = $db->query("SELECT Environment_ID, Name FROM EnvironmentEvent WHERE User_ID = ".$uid." ORDER BY Name;");
		if( !$result ) {
			die( "Failed selecting the environments." );
		}
		while( $line = mysqli_fetch_array( $result, MYSQL_ASSOC ) ) {
			$environments .= "<option value='".$line['Environment_ID']."'>".$line['Name']."</option>";
		}
		
		$db->closeDB();
		return $environments;
	}
	
	function populateRegions() {
		$db = new DBFunctions;
		$db->connectDB();
		$regions = "";
		$result = $db->query("SELECT Region_ID, Name FROM Region ORDER BY Name;");
		if( !$result ) {
			die( "Failed selecting the regions." );
		}
		while( $line = mysqli_fetch_array( $result, MYSQL_ASSOC ) ) {
			$regions .= "<option value='".$line['Region_ID']."'>".$line['Name']."</option>";
		}
		$db->closeDB();
		
		print $regions;
		$db->closeDB();
	}
}
?>